package com.grape.action.system;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.grape.action.common.BaseAction;
import com.grape.domain.system.User;
import com.grape.service.system.UserService;

@Controller
// 如果定义为/user，返回值回向user/index.html跳转导致404，暂不清楚为什么
@Namespace("/")
@ParentPackage("json-default")
@Scope("prototype")
public class UserAction extends BaseAction<User>{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserService us;

	@Action(value="user_save",results=@Result(name="success",type="json"))
	public String save() {
		
		us.save(model,roleIds);
		
		Map<String,Object> mp = new HashMap<>();
		mp.put("msg", model.getUsername()+"保存成功");
		ServletActionContext.getContext().getValueStack().push(mp);
		return SUCCESS;
	}
	
	
	@Action(value="user_list",results=@Result(name="success",type="json"))
	public String list() {
		
		System.out.println("........................."+startTime);
		Pageable pageable = new PageRequest(page-1, rows);
		Page<User> page = us.findAll(pageable,model,startTime,endTime);
		
		pushPageDataToValueStack(page);
		return SUCCESS;
	}
	
	
	@Action(value="user_login",results= {@Result(name="success",type="redirect",location="index.html"),@Result(name="login",type="redirect",location="login.html")})
	public String login() {
		
		// 守门员，一会把要校验的东西交给他
		Subject subject = SecurityUtils.getSubject();
		
		AuthenticationToken token = new UsernamePasswordToken(model.getUsername(),model.getPassword());
		
		try {
			subject.login(token);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return LOGIN;
		}
		
	}
	
	
	
	@Action(value="user_logout",results=@Result(name="success",type="redirect",location="index.html"))
	public String logout() {
		
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
		
		return SUCCESS;
	} 
	
	
	private String[] roleIds;
	private Date startTime;
	private Date endTime;

	
	
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}


	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}


	public void setRoleIds(String[] roleIds) {
		this.roleIds = roleIds;
	}


	
}
